﻿using CommunityToolkit.Mvvm.DependencyInjection;
using HandyControl.Controls;
using Jot.Configuration;
using StickyNote.Core;
using StickyNote.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StickyNote.Views
{
    /// <summary>
    /// NoteWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NoteWindow : GlowWindow, Jot.Configuration.ITrackingAware
    {
        private int windowNo;

        public int WindowNo
        {
            get { return windowNo; }
            set { windowNo = value; }
        }

        public NoteWindow()
        {
            this.InitializeComponent();
        }

        private void NoteWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var jotService = Ioc.Default.GetRequiredService<JotService>();
            jotService.Tracker.Track(this);
        }

        private void ButtonClose_MouseEnter(object sender, MouseEventArgs e)
        {
            this.ButtonClose.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#e81123"));
            this.ButtonClose.Foreground = Brushes.White;
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void ButtonClose_MouseLeave(object sender, MouseEventArgs e)
        {
            this.ButtonClose.Background = Brushes.Transparent;
            this.ButtonClose.Foreground = Application.Current.FindResource("PrimaryTextBrush") as SolidColorBrush;
        }

        private void tbtnTopmost_MouseEnter(object sender, MouseEventArgs e)
        {
            this.tbtnTopmost.Background = Application.Current.FindResource("DarkMaskBrush") as SolidColorBrush;
        }

        private void tbtnTopmost_MouseLeave(object sender, MouseEventArgs e)
        {
            this.tbtnTopmost.Background = Brushes.Transparent;
        }

        public void ConfigureTracking(TrackingConfiguration configuration)
        {
            var cfg = configuration.AsGeneric<NoteWindow>()
                .Id(_ => this.Name, null, false)
                .Properties(w => new { w.Top, w.Width, w.Height, w.Left, w.WindowState, w.Title, w.WindowNo })
                .Property(w => w.txtNote.Text, "Note")
                .PersistOn(nameof(GlowWindow.Closing))
                .StopTrackingOn(nameof(GlowWindow.Closing));
        }
    }
}
